/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.babble.subitem.controller;

import com.babble.subitem.dto.TagSaveDTO;
import com.babble.subitem.dto.TagUpdDTO;
import com.babble.subitem.entity.Tag;
import com.babble.subitem.service.ITagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springblade.common.exception.MsgException;
import org.springblade.core.secure.annotation.PreAuth;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 标签表 控制器
 *
 * @author Blade
 * @since 2021-11-21
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/tag")
@Api(value = "标签表", tags = "标签表接口")
public class TagController {
	private final ITagService tagService;
	@ApiOperation(value = "获取首页所有的标签")
	@PostMapping("list")
	public R<List<Tag>> homePageTags(){
		List<Tag> tags = tagService.selectTags();
		return R.data(tags);
	}
	@ApiOperation(value = "标签被点击时加数量")
	@PostMapping("clickTag")
	public R clickTag(@RequestParam("id")String id) throws MsgException {
		tagService.clickTag(id);
		return R.success("点击成功");
	}
	@ApiOperation(value = "通过ID拿到标签信息")
	@PostMapping("info")
	public R<List<Tag>> info(@RequestParam("id")String id) throws MsgException {
		List<Tag> tag = tagService.info(id);
		return R.data(tag);
	}
	@PreAuth("hasRole('administrator')")
	@ApiOperation(value = "添加标签分类")
	@PostMapping("save")
	public R save(@Valid @RequestBody TagSaveDTO tag) throws MsgException {
		tagService.saveToTag(tag);
		return R.data("添加成功");
	}
	@PreAuth("hasRole('administrator')")
	@ApiOperation(value = "删除标签分类")
	@PostMapping("del")
	public R del(@RequestParam("id")String id) throws MsgException {
		tagService.delToTag(id);
		return R.data("删除成功");
	}
	@PreAuth("hasRole('administrator')")
	@ApiOperation(value = "修改标签分类")
	@PostMapping("upd")
	public R upd(@Valid @RequestBody TagUpdDTO tag) throws MsgException {
		tagService.updToTag(tag);
		return R.data("修改成功");
	}
}
